<!-- cloudMapMap属性 -->
<template>
  <div class="map3d map">
    <div class="map-content">
      <div class="main-text-tran-50">ip地址</div>
      <div class="main-mas-input-simple main-mas-input-simple-input">
        <div class="mas-basic-layout">
          <input
            class="mas-basis-input mas-basis-input2"
            :id="cloudMap.getManagerHostName()"
            placeholder="ws://localhost:8080"
          />
        </div>
      </div>
    </div>
    <div class="edit-btn">
      <div
        class="affirm"
        @click="
          cloudMap.setManagerHost(
            currentElementList[0],
            JQuery('#' + cloudMap.getManagerHostName()).val(),
            false
          );
          createAirCityExplorer();
          history.addHistory(store.clone(), 32);
        "
      >
        连 接
      </div>
    </div>
    <div class="map-content">
      <div class="main-text-tran-50">
        <span>实例</span>
      </div>
      <div class="table">
        <div
          v-for="(item, index) in cloudMap.instanceList"
          :key="index"
          class="label"
          :class="
            item.id === currentElementList[0].data.id ? 'activeTable' : ''
          "
          @click="
            setInstance(item, index);
            history.addHistory(store.clone(), 32);
          "
        >
          <span>{{ item.project }}</span>
        </div>
      </div>
    </div>
    <div class="map-content">
      <div class="main-text-tran-50">
        <span>导览</span>
      </div>
      <div class="table">
        <div
          v-for="(item, index) in cloudMap.viewList"
          :key="index"
          class="label"
          :class="
            item.id === currentElementList[0].data.viewid ? 'activeTable' : ''
          "
          @click="
            playView(item);
            history.addHistory(store.clone(), 34);
          "
        >
          <span>{{ item.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import cloudMap from "@/js/main/attribute/cloudMap.js";
import store from "@/js/store/state.js";
import JQuery from "jquery";
import history from "@/js/main/history.js";
import message from "@/js/util/message.js";
import system from "@/js/main/system.js";
import register from "@/js/main/register.js";
import util from "@/js/util/util.js";

export default {
  data() {
    return {
      cloudMap: cloudMap,
      JQuery: JQuery,
      window: window,
      history: history,
      store: store,
      currentElementList: store.getCurrentElementList(),
    };
  },
  async mounted() {
    await cloudMap.setAttribute(this.currentElementList[0]);
  },
  methods: {
    // ws://192.168.20.22:81
    async createAirCityExplorer() {
      let component = this.currentElementList[0];
      component.data.nodeIP = "";
      component.data.id = "";
      component.data.projectId = "";
      component.data.playerPort = "";
      component.data.viewid = -1;
      component.data.camera = [];
      let url = component.data.url;
      console.log(url);
      cloudMap.instanceList = await cloudMap.getInstanceList(url);
    },
    // 选中实例，开始播放，获取导览列表
    setInstance(row) {
      let component = this.currentElementList[0];
      if (component.data.id === row.id) {
        return;
      }
      component.data.viewid = -1;
      this.handleStartView(row);
      register
        .getComponentById(
          `${system.SYSTEM_EDIT_ELEMENT + this.currentElementList[0].id}`
        )
        .showAirCityExplorer(row);
      let viewList = row.tours;
      let list = new Array();
      if (viewList) {
        for (let a = 0; a < viewList.length; a++) {
          let rs = {};
          rs.name = viewList[a];
          rs.id = a;
          list.push(rs);
        }
      }
      cloudMap.viewList = list;
    },
    // 播放导览
    async playView(row) {
      if (this.currentElementList[0].data.viewid === row.id) {
        this.currentElementList[0].data.viewid = -1;
        await register
          .getComponentById(
            `${system.SYSTEM_EDIT_ELEMENT + this.currentElementList[0].id}`
          )
          .stopAnimation();
        register
          .getComponentById(
            `${system.SYSTEM_EDIT_ELEMENT + this.currentElementList[0].id}`
          )
          .setCamera(this.currentElementList[0].data.camera);
        return;
      }
      let instanceNode = null;
      for (let i = 0; i < cloudMap.instanceList.length; i++) {
        let item = cloudMap.instanceList[i];
        if (item.id == this.currentElementList[0].data.id) {
          instanceNode = util.clone(item);
          break;
        }
      }
      instanceNode.viewid = row.id;
      this.handleStartView(instanceNode);
      register
        .getComponentById(
          `${system.SYSTEM_EDIT_ELEMENT + this.currentElementList[0].id}`
        )
        .showAirCityExplorer(instanceNode);
    },
    handleStartView(instanceNode) {
      this.currentElementList[0].data.nodeIP = instanceNode.nodeIP;
      this.currentElementList[0].data.id = instanceNode.id;
      this.currentElementList[0].data.playerPort = instanceNode.playerPort;
      this.currentElementList[0].data.projectId = instanceNode.projectId;
      this.currentElementList[0].data.viewid = instanceNode.viewid;
      message.message("初始视角设置成功!", "success");
    },
  },
};
</script>
<style lang="scss" scoped>
.map3d {
  padding-left: 10px;
  .main-text-tran-50 {
    font-weight: 600;
    line-height: 30px;
    min-width: 40px;
  }
  .map-content {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
  }
  /* button按钮 */
  .edit-btn {
    display: flex;
    align-content: center;
    justify-content: right;
    .clear {
      margin-top: 10px;
      width: 50%;
      height: 25px;
      line-height: 25px;
      text-align: center;
      color: #ffffff;
      margin: 10px 0;
      background: #303247;
      cursor: pointer;
      border-radius: 5px;
      font-size: 12px;
      &:hover {
        background: #3664ec;
      }
    }
  }
  .table {
    width: 100%;
    max-height: 400px;
    overflow-y: auto;
    overflow-x: hidden;
    border-radius: 10px;
    .activeTable {
      background: #3664ec !important;
    }
    .label {
      width: calc(100% - 4px);
      height: 25px;
      line-height: 25px;
      margin: 3px 2px;
      border-radius: 2px;
      padding-left: 8px;
      cursor: pointer;
      span {
        color: #ffffff;
      }
      &:active {
        background: #3664ec !important;
      }
      &:hover {
        background: #2d2d2d;
      }
    }
  }
}
</style>
